package scala.Functional.ScalaList

import scala.collection.mutable.ListBuffer

object MyList {
   def main(args: Array[String]): Unit = {
      val list = List(1, 2, 3, 4, 5)
      val c = dropNth(2, list)
      println(c)
      val d = dropValue(6, list)
      println(d + "\n")

      val l = List(1, 2, 3, 4)
      val l1 = permutation(l)
      l1.foreach(x => println(x))
      println("\n-------\n")

      val l2 = List('A', 'B', 'C', 'D')
      val l3 = permutation(l2)
      l3.foreach(x => println(x))
   }

   def dropNth(n: Int, l: List[Int]): List[Int] = {
      n match {
         case 0 => l.tail
         case _ => if (n >= l.length) error("ERROR") else l.head :: dropNth(n - 1, l.tail)
      }
   }

   def dropValue[A](x: A, l: List[A]): List[A] = {
      if (l == Nil) {
         println(x + " is not founded ")
         Nil
      } else {
         if (x == l.head) l.tail
         else {
            l.head :: dropValue(x, l.tail)
         }
      }

   }

   /*
    * List Permuatation 
    * @return : List of permuation of input list
    * design by Hung
    */
   def permutation(l: List[AnyVal]): List[List[AnyVal]] = {
      l match {
         case Nil        => Nil
         case List(x)    => List(l)
         case List(x, y) => List(List(x, y), List(y, x))
         case _ => {
            var buf = new ListBuffer[List[AnyVal]]
            l.foreach(x => {
               val l1 = permutation(dropValue(x, l))
               l1.foreach(y => {
                  val l2 = x :: y
                  buf += l2
               })
            })
            buf.toList
         }
      }
   }
}

/*






*/ 